if anon_damage_log_loaded then
	return
end

anon_damage_log_loaded = true

--[[
damageTaken = {
	{ amount, school, timestamp }
}
]]

damageTaken = {}

function CalculateDamageTaken(forSeconds)
	local physDmg = 0
	local magicDmg = 0
	local t_now = GetTime()
	
	for i = #damageTaken, 1, -1 do
		if t_now - damageTaken[i].timestamp > forSeconds then
			break
		end
		if hasbit(damageTaken[i].school, bit_(1)) then
			physDmg = physDmg + damageTaken[i].amount
		else
			magicDmg = magicDmg + damageTaken[i].amount
		end
	end
	
	return physDmg + magicDmg, physDmg, magicDmg
end

--[[
	CC info struct
	Name
	 |
	 |->confuse = counter (resets every 15 secs)
	 |->stun = counter (resets every 15 secs)
	 |->disarm =  counter (resets every 15 secs)
	 |->disorient = counter (resets every 15 secs)
	 |->fear = counter (resets every 15 secs)
	 |->horror = counter (resets every 15 secs)
	 |->randomStun = counter (resets every 15 secs)
	 |->root = counter (resets every 15 secs)
	 |->misc = counter (resets every 15 secs)
--]]
if not CCInfo then CCInfo = {} end

local CLSpellID_To_SpellID = { [118699] = 5782 }

function RefreshCCInfo(UnitGUID, ccType, reverseID, destName)
	if not CCInfo[UnitGUID] then CCInfo[UnitGUID] = {} end

	if not CCInfo[UnitGUID][ccType] then CCInfo[UnitGUID][ccType] = { timestamp = GetTime(), counter = 0} end
	
	if GetTime() - CCInfo[UnitGUID][ccType].timestamp > 15 then
		CCInfo[UnitGUID][ccType].timestamp = GetTime()
		CCInfo[UnitGUID][ccType].counter = 1
	else
		CCInfo[UnitGUID][ccType].counter = CCInfo[UnitGUID][ccType].counter + 1
	end
	
	if debugInfo then print("Refreshed fear CCInfo for:"..destName.."("..UnitGUID..") - (total CC count:"..CCInfo[UnitGUID][ccType].counter..")") end
end

if not fAnon then fAnon = CreateFrame("Frame") end
fAnon:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
fAnon:RegisterEvent("PLAYER_REGEN_ENABLED")
function fAnon_OnEvent(frame, event, ...)
	if event == "PLAYER_REGEN_ENABLED" then
		for k in pairs (damageTaken) do
			damageTaken[k] = nil
		end
	end

	if event == "COMBAT_LOG_EVENT_UNFILTERED" then
		local subEvent = select(2, ...)
		local sourceGUID = select(4, ...)
		local sourceName = select(5, ...)
		local destGUID = select(8, ...)
		local destName = select(9, ...)
		local spellID = select(12, ...)
		local spellName = select(13, ...)
		local spellSchool = select(14, ...)
		local spellAmount = select(15, ...)
		local spellCrit = select(21, ...)

		if debugInfo then print(subEvent.."-"..spellID) end
		
		if	subEvent == "SPELL_DAMAGE"
			or subEvent == "SPELL_PERIODIC_DAMAGE"
			or subEvent == "RANGE_DAMAGE"
			or subEvent == "SWING_DAMAGE"
		then
			if destGUID == UnitGUID("player") then
				if subEvent == "SWING_DAMAGE" then
					spellAmount = select(12, ...)
					spellSchool = 1
				end
				
				tinsert(damageTaken, { amount = spellAmount, school = spellSchool, timestamp = GetTime() })
			end
		end

		if subEvent == "SPELL_CAST_MISSED" then
		end
		
		-- TODO: APPLIED/REFRESH double event when e.g. fear triggered
		if subEvent == "SPELL_AURA_APPLIED" then
			--print(...)
			if destGUID ~= UnitGUID("player") then
				local reverseID = CLSpellID_To_SpellID[spellID]

				if reverseID ~= 0 then
					if fearCC[reverseID] then
						RefreshCCInfo(destGUID, "fear", reverseID, destName)						
					end
				end
			end
		end
		
		if subEvent == "SPELL_AURA_REFRESH" then
			if destGUID ~= UnitGUID("player") then
				local reverseID = CLSpellID_To_SpellID[spellID]
					
				if reverseID ~= 0 then
					if fearCC[reverseID] then
						RefreshCCInfo(destGUID, "fear", reverseID, destName)
					end
				end
			end	
		end
		
		if subEvent == "SPELL_CAST_SUCCESS" then
			local sourceTag
			if sourceGUID ~= UnitGUID("player") then
				sourceTag = "Other"
			else
				sourceTag = "Self"
			end
			if debugInfo then print("["..sourceTag.."] Successfully casted: "..spellName) end
		end
	end
end
fAnon:SetScript("OnEvent", fAnon_OnEvent)
